<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/11/12
 * Time: 下午5:09
 */

namespace Api\Service;


use Mobile\Model\memberModel;
use Mobile\Model\moneylogModel;

class OrderService extends BaseService
{
    protected $orderModel;
    protected $memberModel;
    protected $moneylogModel;
    public function __construct()
    {
        $this->orderModel=M('order');
        $this->memberModel=new memberModel();
    }

    /**查询订单列表
     * @param $where
     * @param $page
     * @param $pageNum
     * @param null $filed
     * @return mixed
     */
    public function queryOrderList($where,$page=1,$pageNum=10,$filed=null){
        if(empty($filed)){
            $filed=self::defaultFiled();
        }
       return $this->orderModel->field($filed)->where($where)->order('order_id desc')->page($page,$pageNum)->select();
    }

    /**
     * 查询订单详情
     * @param $order_id
     * @param null $filed
     * @return mixed
     */
    public function findOrderDetail($order_id,$filed=null){
        if(empty($filed)){
            $filed=self::defaultFiled();
        }
        return $this->orderModel->field($filed)->where(array('order_id'=>$order_id))->find();
    }
    /**
     * 查询订单详情
     * @param $where
     * @param null $filed
     * @return mixed
     */
    public function findOrderDetailByWhere($where,$filed=null){
        if(empty($filed)){
            $filed=self::defaultFiled();
        }
        return $this->orderModel->field($filed)->where($where)->find();
    }

    //查询订单数量
    public function queryCountOrderByWhere($where){
        return $this->orderModel->where($where)->count();
    }

    public function defaultFiled(){
        return 'order_id,
        uid,order_sn,
        pay_orgmoney,
        zhekou_money,
        score_money,
        pay_money,
        cash_money,
        checked,pay_time,
        pay_type,create_time,
        order_title,goods,
        delivery_name,
        delivery_mobile,
        send_time,
        shouhuo_time,
        delivery,
        shipping';
    }

    /**
     * 添加订单
     * @param $addInfo
     * @return mixed
     */
    public function addOrder($addInfo){
       return $this->orderModel->add($addInfo);
    }

    public function backAddOrderInfo(){

    }

    /**
     * 保存订单信息
     * @param $order_id
     * @param $saveInfo
     * @return bool
     */
    public function saveOrderInfo($order_id,$saveInfo){
       return $this->orderModel->where(array('order_id'=>$order_id))->save($saveInfo);
    }


    /*
     * 取消订单加钱流水,并返回用户表里的钱
     * $mtype 1分销佣金,2重消币 ,3现金电子币 ,4重消佣金,5互助奖,6车房奖, 7领导奖, 8分红奖 ,9保证金 , 10个税 , 11提货币流水
     */
    public function orderCancel($money,$order,$mtype){

        $this->moneylogModel=new moneylogModel();
        if($money <= 0){
            return false;//金额等于0 不做任何操作
        }

        $uid = $order['uid'];

        switch($mtype){
            case 2:
                $this->memberModel->jiaQian($uid,'score',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->field('score')->find();
                $yue = $userinfo['score'];
                break;
            case 3:
                $this->memberModel->jiaQian($uid,'cash_money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->field('cash_money')->find();
                $yue = $userinfo['cash_money'];
                break;
            case 1:
                $this->memberModel->jiaQian($uid,'money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->field('money')->find();
                $yue = $userinfo['money'];
                break;
            case 11:
                $this->memberModel->jiaQian($uid,'tihuo_money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->field('tihuo_money')->find();
                $yue = $userinfo['tihuo_money'];
                break;
            case 12:
                $this->memberModel->jiaQian($uid,'prize_money',$money);
                $userinfo = $this->memberModel->where(array('uid'=>$uid))->field('prize_money')->find();
                $yue = $userinfo['prize_money'];
                break;
            default:
                return false;
        }

        $money_log = array(
            "uid" => $order['uid'],
            "nickname" => $order['realname'],
            "logtype" => 0,//收入 1收入 0支出
            "mtype" => $mtype,//类型
            "money" => $money,
            "content" => "【订单取消】".$order['order_title'],
            "order_id" => $order['order_id'],
            "order_uid" => $order['uid'],
            "order_paymoney" => $order['order_cashmoney'],//订单支付的金额(需要分佣的金额)
            "order_nickname" => $order['nickname'],
            "create_time" => date("Y-m-d H:i:s"),//写流水的时间
            "un_lock" => 1,//不锁定,立即扣除 1解锁,0锁定
            "cash_money" => $yue,//查余额
            "note" => "取消订单"
        );


        $res = $this->moneylogModel->addMoneylog($money_log);

        return $res;

    }

    /**
     * 计算运费
     * @param $goods_info
     * @param int $aid
     * @return float|int
     */
    public function get_shipping($goods_info,$aid=330000){
        //计算要支付的钱
        $money = 0;
        $all_weight = 0;//订单总重量
        $s = M("express");
        $where = array(
            "province_id" => $aid
        );
        $res = $s->where($where)->find();
        //首重和续重
        $first = $res["first_weight"];
        $next = $res["next_weight"];
        foreach ($goods_info as $k=>$v){
            //判断是不是包邮商品
            //dump($v['freight_payer']);die;
            if($v['freight_payer'] == 1){
                continue;
            }
            if(empty($v['goods_weight'])){
                $v['goods_weight']=1;
            }
            $all_weight += $v['num'] * $v['goods_weight'];
        }
        $all_weight = ceil($all_weight);

        if($all_weight == 0){
            $money = 0;
        }else if($all_weight<=1){
            $money = $first;
        }else{
            $money = $first + ($all_weight-1)*$next;
        }
        $money = round($money,2);//四舍五入
        return $money;
    }

}